module.exports = class extends think.Model {
  // eslint-disable-next-line camelcase
  async list({ cate_id, name, pageSize = 10, pageNum = 1, order = "id desc" }) {
    const where = {};
    // eslint-disable-next-line camelcase
    if (cate_id) {
      // eslint-disable-next-line camelcase
      where.cate_id = cate_id;
    }

    if (name) {
      where.name = ["like", `%${name}%`];
    }

    const data = await this.where(where)
      .order(order)
      .page(pageNum, pageSize)
      .countSelect();
    return data;
  }
  async save({ id, ...form }) {
    if (id) {
      // eslint-disable-next-line no-return-await
      return await this.where({ id }).update({
        ...form,
        etime: ["exp", "now()"]
      });
    } else {
      // eslint-disable-next-line no-return-await
      return await this.add({
        ...form,
        ctime: ["exp", "now()"],
        etime: ["exp", "now()"]
      });
    }
  }
  async detail(id) {
    const data = await this.find(id);
    const meta = await this.model("wiki_meta")
      .where({ wiki_id: id })
      .find();
    delete meta.id;
    delete meta.wiki_id;
    return {
      ...data,
      ...meta
    };
  }
};
